package 线程.综合练习.多线程之间的比较;


import 线程.综合练习.抽奖箱抽奖.MTh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyFun implements Callable<Integer> {
    //定义一个
    ArrayList<Integer> list;

    public MyFun(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> box = new ArrayList<>();
        while (true) {
            synchronized (MTh.class) {
                //判断奖池是否抽完
                if (list.size() == 0) {
                    System.out.println(Thread.currentThread().getName() + box);
                    int sum = 0;
                    for (Integer integer : box) {
                        sum += integer;
                    }
                    Integer max = Collections.max(box);
                    System.out.println(Thread.currentThread().getName() + "的最高金额为" + max + ",总金额高达" + sum + "元");
                    break;
                } else {
                    //从奖池进行随机抽奖
                    //打乱奖池
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    box.add(prize);
                }
            }
            Thread.sleep(1);
        }
        //返回集合中的最大值
        if (box.size()==0){
            return null;
        }else {
            return Collections.max(box);
        }
    }
}